package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityOrderDetail;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TradeComplainInputActivity extends BaseActivity {

    EntityOrderDetail orderDetail;

    IPresenter presenter;
    @BindView(R.id.iv_back)
    ImageButton ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_complain_reason)
    EditText etComplainReason;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    public static Intent createIntent(Context context, EntityOrderDetail orderDetail) {
        Intent intent = new Intent(context, TradeComplainInputActivity.class);
        intent.putExtra("order", orderDetail);
        return intent;
    }


    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_trade_complain_input;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
        orderDetail = (EntityOrderDetail) getIntent().getSerializableExtra("order");
    }

    @Override
    public void initView() {
        tvTitle.setText("申诉");
    }

    @Override
    public void initOperator() {

    }

    @Override
    public void onOrderRevokeSuccess() {
        super.onOrderRevokeSuccess();
        finish();
    }

    @Override
    public void onOrderRevokeFail(Exception e) {
        super.onOrderRevokeFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_submit:

                String reason = etComplainReason.getText().toString().trim();
                if (TextUtils.isEmpty(reason)) {
                    ToastUtil.getInstance().shortLength(context, "请输入您的申诉理由");
                    return;
                } else {
                    //  申诉
                    JSONObject param = new JSONObject();
                    try {
                        param.put("orderId", orderDetail.getOrderNo());
                        param.put("content", reason);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    presenter.orderRevoke(param.toString());
                }
                break;
        }
    }
}
